<template>
  <div class="resource-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 使用 Form 组件：行内表单 -->
        <el-form
        :inline="true"
        :model="form"
        class="demo-form-inline"
        size="small"
        ref="form">
          <div>
            <el-button type="primary" size="small" @click="onSubmit" :disabled="isLoading">查询搜索</el-button>
            <el-button size="small" @click="onReset">重置</el-button>
          </div>
          <div style="margin-top: 15px;">
            <el-form-item label="资源名称：" prop="name">
              <el-input v-model="form.name" placeholder="资源名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="资源路径：" prop="url">
              <el-input v-model="form.url" placeholder="资源路径" clearable></el-input>
            </el-form-item>
            <el-form-item label="资源分类：" prop="categoryId">
              <el-select v-model="form.categoryId" placeholder="全部" clearable>
                <el-option
                  v-for="item in resourceCategories"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <el-button size="small" @click="addResource()">添加</el-button>
      <el-button size="small" @click="classifyRes()">资源分类</el-button>
      <el-table
        :data="allResourceInfo"
        v-loading="isLoading"
        border
        style="width: 100%">
        <el-table-column
          fixed
          align="center"
          type="index"
          label="编号"
          width="100">
        </el-table-column>
        <el-table-column
          align="center"
          prop="name"
          label="资源名称">
        </el-table-column>
        <el-table-column
          prop="url"
          align="center"
          label="资源路径">
        </el-table-column>
        <el-table-column
          prop="description"
          align="center"
          label="描述">
        </el-table-column>
        <!-- 设置过滤器需要使用作用域插槽获取数据 -->
        <el-table-column
          align="center"
          label="添加时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createdTime | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="250">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)"
              type="text">编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页组件结构 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.current"
      :page-sizes="[10, 15, 20]"
      :page-size="form.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
      :disabled="isLoading">
    </el-pagination>
  </div>
</template>

<script>
import { getResourcePages, getResourceCategories, deleteResource } from '@/services/resource'

export default {
  name: 'ResourceList',
  data () {
    return {
      // 用于存储资源列表数据
      allResourceInfo: [],
      form: {
        current: 1,
        // 每页显示的数据条数
        size: 10,
        // 资源名称
        name: '',
        // 资源路径
        url: '',
        // 资源分类 ID
        categoryId: ''
      },
      // 数据总数
      totalCount: 0,
      // 存储资源分类信息
      resourceCategories: [],
      // 用于保存加载状态
      isLoading: false
    }
  },
  created () {
    // 加载所有资源信息
    this.loadAllResInfo()
    // 加载资源列表
    this.loadResourceCategories()
  },
  methods: {
    // 每页显示条数变化时触发
    handleSizeChange (val) {
      this.form.size = val
      // 由于修改了每页显示的条数，应该将页数还原默认值1
      this.form.current = 1
      this.loadAllResInfo()
    },
    // 页号改变触发
    handleCurrentChange (val) {
      // 将 val 同步给 current
      this.form.current = val
      // 请求更新数据
      this.loadAllResInfo()
    },
    addResource () {
      console.log('添加')
    },
    classifyRes () {
      console.log('资源分类')
    },
    // 重置按钮点击操作
    onReset () {
      this.$refs.form.resetFields()
    },
    // 提交筛选
    onSubmit () {
      // 请求数据前，将请求的页数更新为 1
      this.form.current = 1
      this.loadAllResInfo()
    },
    handleEdit (rowData) {
      // 设置跳转
      console.log(rowData)
    },
    handleDelete (rowData) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await deleteResource(rowData.id)
        console.log(data)
        if (data.code === '000000') {
          this.$message('删除成功!')
          // 更新数据列表
          this.loadAllResInfo()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async loadAllResInfo () {
      // 开始加载数据
      this.isLoading = true
      const { data } = await getResourcePages(this.form)
      if (data.code === '000000') {
        this.allResourceInfo = data.data.records
        this.totalCount = data.data.total
        // 加载完毕，取消加载状态
        this.isLoading = false
      }
    },
    // 加载资源列表
    async loadResourceCategories () {
      const { data } = await getResourceCategories()
      if (data.code === '000000') {
        this.resourceCategories = data.data
      }
    }
  },
  filters: {
    // 日期过滤器
    dateFormat (date) {
      date = new Date(date)
      return `
        ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}
        ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}
      `
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card .el-form {
    .el-button {
      float: right;
      margin-left: 15px;
    }

    div .el-form-item {
      padding-left: 50px;
    }
  }

  .el-table {
    margin-top: 20px;
  }

</style>
